<script setup>
import { h, onMounted, ref, resolveDirective, withDirectives } from 'vue'

import {
  useMessage,
  NButton,
  NForm,
  NFormItem,
  NInput,
  NInputNumber,
  NPopconfirm,
  NTreeSelect,
  NTag,
  NSwitch,
  NFlex,
  NAvatar,
  NSelect,
  NImage,
  NDataTable,
} from 'naive-ui'

import CommonPage from '@/components/page/CommonPage.vue'
import QueryBarItem from '@/components/query-bar/QueryBarItem.vue'
import CrudModal from '@/components/table/CrudModal.vue'
import CrudTable from '@/components/table/CrudTable.vue'
import TheIcon from '@/components/icon/TheIcon.vue'
import { getToken } from '@/utils'
import { renderIcon } from '@/utils'
import { useCRUD } from '@/composables'
// import { loginTypeMap, loginTypeOptions } from '@/constant/data'
import api from '@/api'
import { router } from '@/router'
import default_image from '@/assets/images/mz.jpg'

defineOptions({ name: '佣金发放明细' })

const $table = ref(null)
const queryItems = ref({})
const vPermission = resolveDirective('permission')
const base_path = import.meta.env.VITE_BASE

const {
  modalVisible,
  modalTitle,
  modalLoading,
  handleSave,
  modalForm,
  modalFormRef,
  handleEdit,
  handleDelete,
  handleAdd,
} = useCRUD({
  name: '佣金发放明细',
  initForm: {},
  doCreate: api.createCommission,
  doUpdate: api.updateCommissionMx,
  doDelete: api.deleteCommission,
  refresh: () => $table.value?.handlerefresh(),
})
const isDisabled = ref(false)
const data = reactive({
  attributeOption: [
    {
      id: 1,
      name: '描述模板',
    },
    {
      id: 2,
      name: '规格模板',
    },
  ],
  state_option:[],
  uploadAction: base_path + 'api/goods/brand/file',
  token: getToken(),
  onLevelData:[]
})


onMounted(() => {
  api.getOrdersType().then((res) => (data.state_option = res.data))
  $table.value?.handleSearch()
})

const columns = [
  {
    title: 'ID',
    key: 'id',
    width: 100,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '单号',
    key: 'orders_id',
    width: 40,
    align: 'center',
    ellipsis: { tooltip: true },
  },

  {
    title: "受益人",
    key: 'user_name',
    width: 60,
    align: 'center',
    ellipsis: { tooltip: true },
    fixed: 'left',
  },
  {
    title: '会员类型',
    key: 'user_level_name',
    align: 'center',
    width:100,
    ellipsis: { tooltip: true },
    render(row) {
      return h(
          NTag,
          { type: 'primary', size: 'small', bordered: false, style: { margin: '2px 3px' } },
          { default: () => row.user_level_name }
      )
    },
  },
  {
    title: '商品',
    key: 'goods_name',
    width: 200,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '发生额',
    key: 'goods_price',
    width: 60,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '佣金',
    key: 'price',
    width: 60,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '收益级别',
    key: 'level',
    align: 'center',
    width:60,
    ellipsis: { tooltip: true },
    render(row) {
      return h(
          NTag,
          { type: 'primary', size: 'small', bordered: false, style: { margin: '2px 3px' } },
          { default: () => row.level }
      )
    },
  }  ,
  {
    title: '培育模板',
    key: 'grow_id_name',
    align: 'center',
    width:60,
    ellipsis: { tooltip: true },
    render(row) {
      return h(
          NTag,
          { type: 'primary', size: 'small', bordered: false, style: { margin: '2px 3px' } },
          { default: () => row.grow_id_name }
      )
    },
  },
  {
    title: '产生时间',
    key: 'created_at',
    width: 140,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '操作',
    key: 'actions',
    width: 200,
    align: 'center',
    fixed: 'right',
    render(row) {

      return [
        withDirectives(
            h(
                NButton,
                {
                  size: 'small',
                  type: 'error',
                  style: 'margin-right: 8px;',
                  onClick: () => {
                    handleEdit(row)
                  },
                },
                {
                  default: () => '修改佣金',
                }
            ),
            [[vPermission, 'post/api/content/category/update']]
        )
      ]



    },
  },
]


function formatTimestamp(timestamp) {
  const date = new Date(timestamp)
  const pad = (num) => num.toString().padStart(2, '0')
  const year = date.getFullYear()
  const month = pad(date.getMonth() + 1) // 月份从0开始，所以需要+1
  const day = pad(date.getDate())
  const hours = pad(date.getHours())
  const minutes = pad(date.getMinutes())
  const seconds = pad(date.getSeconds())
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
const datetimeRange = ref()
const handleDateRangeChange = (value) => {
  queryItems.value.start_time = formatTimestamp(value[0])
  queryItems.value.end_time = formatTimestamp(value[1])
}

</script>

<template>
  <!-- 业务页面 -->
  <CommonPage show-footer title="佣金发放明细">
    <template #action>
      <div>

      </div>
    </template>
    <!-- 表格 -->
    <CrudTable
        ref="$table"
        v-model:query-items="queryItems"
        :columns="columns"
        :get-data="api.getCommissionIncome"
        :scroll-x="1200"
    >
      <template #queryBar>
        <QueryBarItem label="受益人姓名" :label-width="80">
          <NInput
              v-model:value="queryItems.user_name"
              clearable
              type="text"
              placeholder="请输入受益人姓名"
              @keypress.enter="$table?.handleSearch()"
          />
        </QueryBarItem>
        <QueryBarItem label="商品名称" :label-width="80">
          <NInput
              v-model:value="queryItems.goods_name"
              clearable
              type="text"
              placeholder="请输入商品名称"
              @keypress.enter="$table?.handleSearch()"
          />
        </QueryBarItem>
      </template>
    </CrudTable>

    <!-- 新增/编辑 弹窗 -->
        <CrudModal v-model:visible="modalVisible" :title="modalTitle" :loading="modalLoading" @save="handleSave"
                   style="width: 350px !important;"
        >
          <NForm ref="modalFormRef" label-placement="left" label-align="left" :label-width="80"
                 :model="modalForm" :rules="validateAddUser"  >

            <NFormItem label="佣金" path="price" style="width: 100%;">
              <NInput v-model:value="modalForm.price" clearable placeholder="请输入佣金" />
            </NFormItem>

          </NForm>
        </CrudModal>


  </CommonPage>
</template>
